﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace IISLogAnalytics
{
    public partial class PageComment : GlassForm
    {
        #region Attributes

        private int type = 0;   //0为新增，1为修改

        #endregion

        #region PageComment

        public PageComment()
        {
            InitializeComponent();
            DataTable dt = DBO.getSiteList();
            this.cbbSiteName.DisplayMember = "domain";
            this.cbbSiteName.ValueMember = "sitename";
            this.cbbSiteName.DataSource = dt;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = AeroColor;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.pComment.Location = new Point((this.Width - this.pComment.Width) / 2, (this.Height - this.pComment.Height) / 4);
        }

        #endregion

        #region Event

        #region ComboBox

        private void cbbSiteName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = DBO.getPageCommentList(this.cbbSiteName.SelectedValue.ToString(), this.cbbSiteName.GetItemText(this.cbbSiteName.SelectedItem));
            this.gvPage.AutoGenerateColumns = false;
            this.gvPage.Columns[0].DataPropertyName = "domain";
            this.gvPage.Columns[1].DataPropertyName = "uri";
            this.gvPage.Columns[2].DataPropertyName = "comment";
            this.gvPage.DataSource = dt;
            this.gvPage.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        #endregion

        #region DataGridView

        private void gvPage_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}条记录", this.gvPage.RowCount);
        }

        #endregion

        #region GlassButton

        private void gbtnAdd_Click(object sender, EventArgs e)
        {
            type = 0;
            this.lblSiteName.Text = this.cbbSiteName.GetItemText(this.cbbSiteName.SelectedItem);
            this.pComment.Visible = true;
            this.cbbSiteName.Enabled = false;
        }

        private void gbtnModify_Click(object sender, EventArgs e)
        {
            if (this.gvPage.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的记录");
                return;
            }
            if (this.gvPage.SelectedRows.Count > 1)
            {
                MessageBox.Show("每次只能修改一条记录");
                return;
            }

            type = 1;
            this.lblSiteName.Text = this.cbbSiteName.GetItemText(this.cbbSiteName.SelectedItem);
            this.tbURL.Text = this.gvPage.SelectedRows[0].Cells[1].Value.ToString();
            this.tbComment.Text = this.gvPage.SelectedRows[0].Cells[2].Value.ToString();
            this.pComment.Visible = true;
            this.cbbSiteName.Enabled = false;
        }

        private void gbtnDelete_Click(object sender, EventArgs e)
        {
            List<string> urls = new List<string>();
            foreach (DataGridViewRow row in this.gvPage.SelectedRows)
            {
                urls.Add(row.Cells[1].Value.ToString());
            }
            DBO.DeletePageComment(this.cbbSiteName.SelectedValue.ToString(), urls);
            cbbSiteName_SelectedIndexChanged(sender, e);
        }

        private void gbtnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.cbbSiteName.SelectedValue.ToString()))
            {
                MessageBox.Show("数据异常");
                return;
            }
            if (this.tbURL.Text.Length == 0)
            {
                MessageBox.Show("请输入网址");
                return;
            }
            if (this.tbComment.Text.Length == 0)
            {
                MessageBox.Show("请输入备注信息");
                return;
            }

            if (type == 0)
            {
                switch (DBO.AddPageComment(this.cbbSiteName.SelectedValue.ToString(), this.tbURL.Text, this.tbComment.Text))
                {
                    case -2:
                        MessageBox.Show("记录已存在，请检查");
                        return;
                    case -1:
                        MessageBox.Show("操作失败");
                        return;
                    default:
                        MessageBox.Show("添加成功");
                        break;
                }
            }
            else
            {
                switch (DBO.UpdatePageComment(this.cbbSiteName.SelectedValue.ToString(), this.tbURL.Text, this.tbComment.Text))
                {
                    case 0:
                        MessageBox.Show("记录不存在，请检查");
                        return;
                    case -1:
                        MessageBox.Show("操作失败");
                        return;
                    default:
                        MessageBox.Show("添加成功");
                        break;
                }
            }

            cbbSiteName_SelectedIndexChanged(sender, e);
            this.pComment.Visible = false;
            ClearPanel();
        }

        private void gbtnCancel_Click(object sender, EventArgs e)
        {
            this.pComment.Visible = false;
            ClearPanel();
        }

        #endregion

        #endregion

        #region Method

        private void ClearPanel()
        {
            this.lblSiteName.Text = "";
            this.tbURL.Text = "";
            this.tbURL.Text = "";
            this.cbbSiteName.Enabled = true;
        }

        #endregion
    }
}
